package cn.zcf.pet.service.impl;

import cn.zcf.basic.exception.BusinessException;
import cn.zcf.basic.service.IBaiduAiAuditService;
import cn.zcf.basic.service.impl.BaseServiceImpl;
import cn.zcf.basic.utils.*;
import cn.zcf.order.domain.OrderPet;
import cn.zcf.order.mapper.OrderPetMapper;
import cn.zcf.org.domain.Emp;
import cn.zcf.org.domain.Shop;
import cn.zcf.org.mapper.EmpMapper;
import cn.zcf.org.mapper.ShopMapper;
import cn.zcf.pet.domain.Pet;
import cn.zcf.pet.domain.SearchMessage;
import cn.zcf.pet.domain.SearchMessageLog;
import cn.zcf.pet.dto.SearchDto;
import cn.zcf.pet.mapper.PetMapper;
import cn.zcf.pet.mapper.SearchMessageLogMapper;
import cn.zcf.pet.mapper.SearchMessageMapper;
import cn.zcf.pet.query.SearchMessageQuery;
import cn.zcf.pet.service.ISearchMessageService;
import cn.zcf.user.domain.Logininfo;
import cn.zcf.user.mapper.UserMapper;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@Service
public class SearchMessageServiceImpl extends BaseServiceImpl<SearchMessage> implements ISearchMessageService {
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private SearchMessageMapper searchMessageMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private SearchMessageLogMapper searchMessageLogMapper;
    @Autowired
    IBaiduAiAuditService baiduAiAuditService;
    @Autowired
    private EmpMapper empMapper;
    @Autowired
    private OrderPetMapper orderPetMapper;
    @Autowired
    private PetMapper petMapper;
    @Override
    public void publish(SearchMessage s, HttpServletRequest request) {
        Point point = DistanceUtils.getPoint(s.getAddress());
        Shop shop = DistanceUtils.getNearestShop(point, shopMapper.loadAll());
        s.setShop_id(shop.getId());
        Logininfo loginInfo = LoginContext.getLoginInfo(request);
        Long id = userMapper.loadByLoginInfoId(loginInfo.getId()).getId();
        s.setUser_id(id);
        searchMessageMapper.add(s);
        //2.智能审核
        Boolean textBoo = baiduAiAuditService.textAudit(s.getTitle() + s.getName() + s.getAddress());
        Boolean imageBoo = true/*baiduAiAuditService.imageAudit(s.getResources())*/;
        if(textBoo && imageBoo){
            s.setState(1);
            s.setNote("审核成功");
            searchMessageMapper.update(s);
            SearchMessageLog log = new SearchMessageLog();
            log.setState(1);
            log.setNote("审核成功");
            log.setMsg_id(s.getId());
            searchMessageLogMapper.add(log);
        }else{
            s.setState(0);
            s.setNote("信息异常审核失败");
            searchMessageMapper.update(s);
            SearchMessageLog log = new SearchMessageLog();
            log.setState(0);
            log.setNote("审核失败");
            log.setMsg_id(s.getId());
            searchMessageLogMapper.add(log);
        }
    }

    @Override
    public PageList<SearchMessage> toHandle(SearchMessageQuery query, HttpServletRequest request) {
        Logininfo logininfo = LoginContext.getLoginInfo(request);
        Emp emp = empMapper.loadByLoginInfoId(logininfo.getId());
        query.setState(1);
        query.setShop_id(emp.getShop_id());
        return new PageList<SearchMessage>(searchMessageMapper.queryCount(query),searchMessageMapper.queryData(query));
    }

    @Override
    public void reject(Long id) {
        searchMessageMapper.reject(id);
    }

    @Override
    public void accept(SearchDto dto, HttpServletRequest request) {
        Long msgId = dto.getMsgId();
        OrderPet orderPet = orderPetMapper.loadByMsgId(msgId);
        if(orderPet!=null){
            throw new BusinessException("订单正在处理中。。。");
        }
        Emp emp = empMapper.loadById(dto.getHandler());
        SearchMessage message = searchMessageMapper.loadById(msgId);
        Pet pet = searchMessage2Pet(message);
        //创建宠物收购订单
        OrderPet order = buildPetAcquisitionOrder(message,emp,pet);
        orderPetMapper.add(order);
    }

    private OrderPet buildPetAcquisitionOrder(SearchMessage message, Emp emp, Pet pet) {
        OrderPet order = new OrderPet();
        String orderSn = CodeGenerateUtils.generateOrderSn(emp.getId());
        order.setOrderSn(orderSn);
        order.setDigest(message.getName()+"的收购订单");
        //最后收购是当前时间+2两天
        long lastcomfirmtimeLong = System.currentTimeMillis()+1000*60*60*24*2;
        order.setLastcomfirmtime(new Date(lastcomfirmtimeLong));
        order.setState(0);
        order.setPrice(message.getPrice());
        //        order.setPaytype(); //现金支付
        order.setPet_id(pet.getId());
        order.setUser_id(message.getUser_id());
        order.setShop_id(pet.getShop_id());
        order.setAddress(message.getAddress());
        order.setEmp_id(emp.getId());
        order.setSearch_master_msg_id(message.getId());
        return order;
    }

    private Pet searchMessage2Pet(SearchMessage message) {
        Pet pet = new Pet();
        pet.setName(message.getName());
        pet.setCostprice(message.getPrice());
        pet.setType_id(message.getPet_type());
        pet.setResources(message.getResources());
        pet.setState(message.getState());
        pet.setShop_id(message.getShop_id());
        pet.setUser_id(message.getUser_id());
        pet.setSearch_master_msg_id(message.getId());
        petMapper.add(pet);
        return pet;

    }
}
